package com.breakfast.fun.ui.user.recharge;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.ui.user.recharge.rechargecard.RechargeCardFragment;
import com.breakfast.fun.ui.user.recharge.rechargemoney.RechargeMoneyFragment;
import com.breakfast.fun.ui.user.recharge.rechargerecord.RechargeRecordActivity;
import com.breakfast.fun.ui.user.recharge.rechargexchangecode.RechargeExchangeCodeFragment;
import com.breakfast.fun.view.widget.TitleBarView;

import butterknife.Bind;

public class RechargeActivity extends BaseActivity<RechargePresenter, RechargeModel> implements RechargeContract.View {


    @Bind(R.id.tbv_recharge)
    TitleBarView tbvRecharge;
    @Bind(R.id.rb_recharge_money)
    RadioButton rbRechargeMoney;
    @Bind(R.id.rb_recharge_card)
    RadioButton rbRechargeCard;
    @Bind(R.id.rb_recharge_exchange_code)
    RadioButton rbRechargeExchangeCode;
    @Bind(R.id.rg_recharge_type)
    RadioGroup rgRechargeType;
    @Bind(R.id.fl_recharge)
    FrameLayout flRecharge;

    private RechargeMoneyFragment moneyRecharge;
    private RechargeCardFragment cardRecharge;
    private RechargeExchangeCodeFragment exchangeCodeRecharge;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    public void initView() {

        tbvRecharge.setDoubleTitle("在线充值", "记录");
        tbvRecharge.setBack(this);

        setTabSelection(0);

        rgRechargeType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_recharge_money:
                        setTabSelection(0);
                        break;
                    case R.id.rb_recharge_card:
                        setTabSelection(1);
                        break;
                    case R.id.rb_recharge_exchange_code:
                        setTabSelection(2);
                        break;
                    default:
                        setTabSelection(0);
                        break;
                }
            }
        });


        tbvRecharge.setRightTextListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RechargeActivity.this, RechargeRecordActivity.class);
                startActivity(intent);
            }
        });

    }

    private void setTabSelection(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (moneyRecharge == null) {
                    moneyRecharge = new RechargeMoneyFragment();
                    transaction.add(R.id.fl_recharge, moneyRecharge);
                } else {
                    transaction.show(moneyRecharge);
                }
                break;
            case 1:
                if (cardRecharge == null) {
                    cardRecharge = new RechargeCardFragment();
                    transaction.add(R.id.fl_recharge, cardRecharge);
                } else {
                    transaction.show(cardRecharge);
                }
                break;
            case 2:
                if (exchangeCodeRecharge == null) {
                    exchangeCodeRecharge = new RechargeExchangeCodeFragment();
                    transaction.add(R.id.fl_recharge, exchangeCodeRecharge);
                } else {
                    transaction.show(exchangeCodeRecharge);
                }
                break;
        }
        transaction.commit();
    }

    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction) {
        if (moneyRecharge != null) {
            transaction.hide(moneyRecharge);
        }
        if (cardRecharge != null) {
            transaction.hide(cardRecharge);
        }
        if (exchangeCodeRecharge != null) {
            transaction.hide(exchangeCodeRecharge);
        }
    }


}
